﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using OVS.Business;
using OVS.CoreDomain;
using OVS.CoreDomain.Business;

namespace OVS
{
    public partial class RegisterCandidate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoggedIn"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            else
            {
                ILoggedin loggedIn = (ILoggedin)Session["LoggedIn"];
                if (!loggedIn.IsAdmin)
                {
                    Response.Write("You must be logged in as Admin to view this page!");
                    Response.End();
                }
            }

            if (Request.QueryString["Mode"] != null)
            {
                string _mode = Request.QueryString["Mode"].ToString();
                ViewState["Mode"] = _mode;
                string _candidateId = Request.QueryString["candidateId"].ToString();
                hidCandidateId.Value = _candidateId;
                PopulateCandidate(_candidateId);
            }
        }

        protected void PopulateCandidate(string candidateId)
        {
          
            IBusinessCandidate businessCandidate= new BusinessCandidate();
            ICandidate candidate = businessCandidate.GetCandidate( int.Parse(candidateId));

            PopulateVoterDetails(candidate.Voter.VoterId);
            txtVoterId.Text = candidate.Voter.VoterId;
            btnSearch.Enabled = false;
            ((DropDownList)SelectWard.FindControl("ddlDistrict")).SelectedValue = candidate.Constituency.District.DistrictId;

            SelectWard.FillPanchayat(candidate.Constituency.District.DistrictId);

            ((DropDownList)SelectWard.FindControl("ddlPanchayat")).SelectedValue = candidate.Constituency.Panchayat.PanchayatId;
            SelectWard.FillWards(candidate.Constituency.Panchayat.PanchayatId);

            ((DropDownList)SelectWard.FindControl("ddlWard")).SelectedValue = candidate.Constituency.Ward.WardId ;

            txtParty.Text = candidate.Party;

            imgSymbol.ImageUrl = Page.ResolveClientUrl(@"~/Photos/CandidateSymbols/" + candidate.Symbol + ".jpg");

            imgSymbol.Visible = true;

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ICandidate candidate = new Candidate();

            candidate.CandidateId = int.Parse(hidCandidateId.Value);
            IConstituency constituency = new Constituency();

            IDistrict district = new District();            
            district.DistrictId = ((DropDownList)SelectWard.FindControl("ddlDistrict")).SelectedValue;

            IPanchayat panchayat = new Panchayat();
            panchayat.PanchayatId = ((DropDownList)SelectWard.FindControl("ddlPanchayat")).SelectedValue;

            IWard ward = new Ward();
            ward.WardId= ((DropDownList)SelectWard.FindControl("ddlWard")).SelectedValue;

            constituency.District = district;
            constituency.Panchayat = panchayat;
            constituency.Ward = ward;

            candidate.Constituency = constituency;
            candidate.Party = txtParty.Text;
            IVoter voter = new Voter();
            voter.VoterId = txtVoterId.Text;

            IElection election = new Election();
            election.ElectionId = 1;

            candidate.Voter = voter;
            candidate.Constituency.Election = election;
            candidate.Symbol = txtVoterId.Text + constituency.District.DistrictId + constituency.Panchayat.PanchayatId + constituency.Ward.WardId;

            IBusinessCandidate businessCandidate = new BusinessCandidate();
            if ((ViewState["Mode"] != null) && (ViewState["Mode"].ToString() == "Edit"))
                businessCandidate.UpdateCandidate(candidate);
            else
                businessCandidate.AddCandidate(candidate);  

            
         

            if (upSymbol.HasFile)
            {
                string _photoName = candidate.Symbol + ".jpg";
                string _mapPath = Server.MapPath("~/Photos/CandidateSymbols/" + _photoName);
                upSymbol.PostedFile.SaveAs(_mapPath);
            }

            //Reset();

           

            lblMsg.Text = "Candidate saved successfuly";

             
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PopulateVoterDetails(txtVoterId.Text);
        }

        protected void PopulateVoterDetails(string _voterId)
        {
            IBusinessVoter businessVoter = new BusinessVoter();
            IVoterSearchCriteria voterSearchCriteria = new VoterSearchCriteria();
            voterSearchCriteria.VoterId = _voterId;
            IList<IVoter> voters = businessVoter.SearchVoters(voterSearchCriteria);
            gvResults.DataSource = voters;
            gvResults.DataBind();

            imgVoterPhoto.ImageUrl = Page.ResolveClientUrl(@"~/Photos/Voters/" + _voterId + ".jpg");
            imgVoterPhoto.Visible = true;

        }
    }
}
